"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const mongoHelper = require("./service/mongoHelper");
const apiHelper = require("./service/apiHelper");
const InspectRecord_1 = require("./model/InspectRecord");
const imageUploader = require("./service/imageUploader");
const lineNameList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, 17].map((x) => `${x}号线`);
const SEPERATOR = "～";
function runAsync() {
    return __awaiter(this, void 0, void 0, function* () {
        let { client, db } = yield mongoHelper.openMongoAsync();
        for (const lineName of lineNameList) {
            console.log(`start process ${lineName}`);
            //先从顺凯那里拉数据
            let line = yield apiHelper.fetchLineFromRemoteAsync(lineName);
            //开始进行比对
            for (let section of line.sections) {
                let sectionName = `${section.metroStationNameStart}${SEPERATOR}${section.metroStationNameEnd}`;
                let sectItem = yield apiHelper.getSectionAsync(sectionName); //我们自己数据库对该section的定义
                if (!sectItem)
                    continue;
                for (let prj of section.projects) {
                    let syncRecord = { name: prj.name, date: prj.projTaskLastedResultDate, section: sectionName, sectionName };
                    let isSynced = yield mongoHelper.isRecordSyncAlready(db, syncRecord); //是否同步过
                    if (isSynced) {
                        console.log(`continue : ${JSON.stringify(syncRecord)}  已经同步过，不处理`);
                        continue; //同步过就不处理了
                    }
                    let tobeUpdateRecord = InspectRecord_1.default.parseFromSkyFormat(prj, sectItem); //转换一下数据格式，大家字段定义都各不相同
                    tobeUpdateRecord.imageList = yield imageUploader.copyImagesFromSkyToQiniu(tobeUpdateRecord.imageList); //转存图片到七牛去
                    // fs.writeFileSync('./temp.txt', JSON.stringify(tobeUpdateRecord) + '\r\n', { flag: 'a' }) /** @todo 即将被删除 */
                    yield apiHelper.uploadToMetroProjects(tobeUpdateRecord); //存我们自己数库
                    yield mongoHelper.saveSyncRecord(db, syncRecord); //存入同步中
                }
            }
            yield mongoHelper.updateLineAysnc(db, lineName, line);
            console.log(`finish ${lineName}`);
        }
        yield mongoHelper.closeMongoAsync(client);
        console.log('all finish');
    });
}
exports.runAsync = runAsync;
let timeout = null;
function start(hour = 24) {
    runAsync();
    timeout = setTimeout(() => {
        start(hour);
    }, hour * 3600 * 1000); //一天运行一次
}
exports.start = start;
function stop() {
    if (!timeout)
        return;
    clearTimeout(timeout);
}
exports.stop = stop;
